package com.hellpro.lolbox.home.weight;


import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.widget.ListView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hellpro.lolbox.R;
import com.hellpro.lolbox.base.BaseFragment;
import com.hellpro.lolbox.bean.ZXSS;
import com.hellpro.lolbox.config.UrlConfigs;
import com.hellpro.lolbox.home.HomeJsonUtils;
import com.hellpro.lolbox.home.model.OnZXListListener;
import com.hellpro.lolbox.home.presenter.ZXCommListAdapter;
import com.hellpro.lolbox.home.presenter.ZXFragment;
import com.hellpro.lolbox.home.view.IZXCommAFragment;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class ZXCommAFragmentC extends BaseFragment implements IZXCommAFragment {
    private static final  int TT =0;
    private static final  int SP=1;
    private static final  int SS=2;
    private String mPath = UrlConfigs.ZXBS;
    private  int page = 1;
    private int pageSize;

    @BindView(R.id.zx_commonac)
    PullToRefreshListView zxCommonac;
    private ZXCommListAdapter zxCommListAdapter;




    @Override
    public void initView() {
      zxCommonac.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
          @Override
          public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
              String path = mPath+ 1;
              goDown(path);
              zxCommonac.postDelayed(new Runnable() {
                  @Override
                  public void run() {
                      zxCommonac.onRefreshComplete();
                  }
              },7000);
          }

          @Override
          public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

              page ++;
              if (page>pageSize){
                  Toast.makeText(getFActivity(),"已到最后一页",Toast.LENGTH_SHORT).show();
                  return;
              }
              String path = mPath+ page;
              goDown(path);
              zxCommonac.postDelayed(new Runnable() {
                  @Override
                  public void run() {
                      zxCommonac.onRefreshComplete();
                  }
              },7000);
          }
      });

        zxCommListAdapter = new ZXCommListAdapter();

        zxCommonac.setAdapter(zxCommListAdapter);

                    String path = UrlConfigs.ZXBS + 1;
                    goDown(path);
        }




    @Override
    public int getLayoutId() {
        return R.layout.fragment_zxcomm_ac;
    }




    @Override
    public void hideProgress() {
        zxCommonac.onRefreshComplete();
    }



    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what ==3){
                Object obj = msg.obj;
                ZXSS zzss = (ZXSS) obj;
              pageSize = zzss.getPageSize();
                if (zxCommonac!=null) {
                    zxCommonac.onRefreshComplete();
                }
                zxCommListAdapter.setZXData(obj,2);
            }
        }
    };


    //开启下载
    private void goDown(String path){
        new ZXFragment(getFActivity(), path, new OnZXListListener() {
            @Override
            public void loadSuccees(String json) {
                ZXSS data = HomeJsonUtils.getData(json, ZXSS.class);
                Message obtain = Message.obtain();
                obtain.what=3;
                obtain.obj= data;
                mHandler.sendMessage(obtain);

            }

            @Override
            public void loadFail() {

            }
        });

    }
}





